// 登录/注册切换
$('.tab_0517').delegate('li','click',function(){
    var _index = $(this).index();
    $(this).addClass('current').siblings().removeClass('current').parent().nextAll('.cont_iput').eq(_index).show().siblings('.cont_iput').hide();
});

function switchTab(i) {
    $(".tab_0517 li").eq(i).click();
}


$.each($('.inputtxt_1'),function (i,v){
    $(v).attr('placeholder',['手机号','密码','确认密码','手机号','密码','手机号或邮箱','密码','验证码'][i])
});

var JPlaceHolder = {
    
    _check : function(){
        return 'placeholder' in document.createElement('input');
    },
    
    init : function(t){
        if(!this._check()){
            this.fix(t);
        }
    },
    
    fix : function(t){
        jQuery(':input[placeholder]').each(function(index, element) {
            var self = $(this), txt = self.attr('placeholder');
            self.wrap($('<div></div>').css({position:'relative', zoom:'1', border:'none', background:'none', padding:'none', margin:'none'}));
            var pos = self.position(), h = t==undefined ? self.outerHeight() : t, paddingleft = self.css('padding-left');
            var holder = $('<span></span>').text(txt).css({position:'absolute', left:pos.left, top:pos.top, height:h, lineHeight:h+'px', paddingLeft:paddingleft, color:'#aaa',cursor:'text'}).appendTo(self.parent());
            self.keyup(function(e) {
                if(self.val()==''){
                    holder.show();
                }else{
                    holder.hide();
                }
            });
            holder.click(function(e) {
                setTimeout(function(){self.focus()}, 0) 
            });
        });
    }
};

jQuery(function(){
    JPlaceHolder.init(42);    
});

//职业词选项卡
if($('.tabs_con')[0]){
    (function(){
        var t = $('.tabs_h'),
            l = $('.tabs_i>ul>li');
        t.delegate('li','click',function(){
            $(this).addClass('cur_cc').siblings().removeClass('cur_cc');
            l.eq($(this).index()).fadeIn(300).siblings().fadeOut(200);
        })
    })()
}


// 注册验证
//var email = new MZ_REG.formEle(true, "email", null, "email_info",
//['', '', '请输入正确的邮箱或手机号！', 'E-mail地址长度不能超过100位！', '请输入正确的邮箱或手机号！',
//'该手机号或邮箱已被注册，请直接<a onclick="switchTab(1)" class="login_i"  href="javascript:void(0)">登录</a>', '该手机号或邮箱已被注册，请直接<a onclick="switchTab(1)" class="login_i"  href="javascript:void(0)">登录</a>', '雅虎邮箱即将停止服务，请更换为其他','您的访问过于频繁，请稍后再试'], 0, true);

// 注册验证 注册时只允许手机号并对手机号校验
// var email = new MZ_REG.formEle(true, "email", null, "email_info",
//     ['', '', '请输入正确的手机号！', '', '请输入正确的手机号！',
//     '该手机号已被注册，请直接<a onclick="switchTab(1)" class="login_i"  href="javascript:void(0)">登录</a>', '该手机号已被注册，请直接<a onclick="switchTab(1)" class="login_i"  href="javascript:void(0)">登录</a>', ''], 0, true, true);

// var password1 = new MZ_REG.formEle(true, "password", null, "password1_info", ['', '', '请输入密码！', '密码只能是6-20位的字母数字和下划线！'], 0);
// var password2 = new MZ_REG.formEle(true, "mirror", "sameas=password1", "password2_info", ['', '', '请重输密码！', '两次输入密码不相同！'], 0);
// var accept = new MZ_REG.formEle(true, "checkbox", null, "accept_info", ['', '', '请接受智联招聘的用户协议和隐私政策！']);
// //new start                                                                      
// //0验证通过1未输入手机号2未输入验证码3您输入的验证码已过期，请重新获取4手机验证码不正确
// var verifyCode = new MZ_REG.formEle(true, "verifyCode", null, "verifyCode_info", ['', '', '请输入验证码', '您输入的验证码已过期，请重新获取', '您输入的验证码有误', '验证码已发送，请查收短信！','验证码格式错误'], 0, true);
// // var makeCode = new MZ_REG.formEle(true, "makeCode", null, "verifyCode_info", ['', '参数不正确', '请输入正确的手机号', '该手机已注册', '获取验证码太频繁，请稍后再试', '缓存失败', '短信发送失败', '发生异常'], 0);
// var mailverifyCode = new MZ_REG.formEle(true, "mailverifyCode", null, "mailverifyCode_info", ['', '', '请输入验证码', '您输入的验证码已过期，请重新获取', '您输入的验证码有误', '验证码已发送，请查收短信！','验证码格式错误'], 0, true);


// var regForm = new MZ_REG.iniForm('regform');

var needflag1=!0,needflag2=!0;

// 登录验证
//var loginname  = new MZ_REG.formEle(true,"email",null,"loginname_info",['','','请输入E-mail！','E-mail地址长度不能超过100位！','E-mail格式错误','请输入E-mail！'],0);

// var loginname = new MZ_REG.formEle(true, "email", null, "loginname_info",
// ['', '', '登录名不能为空', 'E-mail地址长度不能超过100位！', '您输入的手机号或邮箱格式不正确！',
// '手机号或邮箱不存在，请确认后重新输入！', '', '雅虎邮箱即将停止服务，请更换为其他','您的访问过于频繁，请稍后再试','您的账户被暂时锁定，请12小时后重试','手机号或邮箱不存在，请确认后重新输入！'], 0, true);


// //var loginname = new MZ_REG.formEle(true, "email", null, "loginname_info", ['', '', '账号不存在，请确认后重新输入'], 0);
// var password = new MZ_REG.formEle(true, "password", null, "password_info", ['', '', '密码不能为空', '密码只能是6-20位的字母数字和下划线！', '您输入的密码和账号不匹配'], 0);

// var _mailverifyCode = new MZ_REG.formEle(true, "_mailverifyCode", null, "_mailverifyCode_info", ['', '', '密码不能为空', '您输入的验证码已过期，请重新获取', '请输入正确的验证码', '验证码已发送，请查收短信！','验证码格式错误','您的操作过于频繁，请输入验证码'], 0, true);

// var loginForm = new MZ_REG.iniForm('loginform');



function checkRegForm() {
    var f = document.forms["regform"];
    var flag = regForm.checkForm();
    if (flag)
        f.regbutton.disabled = true;
    return flag;
}

function checkLoginForm() {
    var f = document.forms["loginform"];
    var flag = loginForm.checkForm();
    if (flag) f.loginbutton.disabled = true;
    return flag;
}


//注册
function loginAction() {
    if (!checkRegForm()) {
        return;
    } else {
        var _regFrom = $('#regform');
        _regFrom[0].regbutton.disabled = false;
        var data = _regFrom.serialize();
        data = { RegisterName: $("#email").val(), Password: $("#password1").val()
                    , CheckCode: $("#mailverifyCode").val(), MobileValidCode: $("#verifyCode").val()
                    , PasswordConfirm: $("#password2").val()
                };
        var url = '{{/home/login}}';
        $.ajax({
            type: "get",
            data: data,
            url: url,
            async: false,
            dataType: "jsonp",
            success: function (r,s,xhr) {
                //注册成功 = 0
                //参数验证错误 = -1
                //请输入手机验证码 = -2
                //手机验证码验证失败 = -3
                //账户格式错误 = -15,
                
                //IP被列为黑名单 = -21,
                //邮箱被列为黑名单 = -22,
                //邮箱域名被列为黑名单 = -23,

                //申请过于频繁 = -31,
               //图片验证码输入为空 = -32,
                //图片验证码输入错误 = -33,
                //用户名不符合规范 = -41,
                //密码不符合规范 = -42,
                //该用户名已经被注册 = -51,
                //注册失败 = -500,

                var code=r.ErrorCode;

                //IP被列为黑名单 = -21,
                //邮箱被列为黑名单 = -22,
                //邮箱域名被列为黑名单 = -23,
                if(code==-21||code==-22||code==-23){
                    alert("注册失败，请稍候重试！");
                    return;
                }

                switch(code){
                    case 0:
                    //注册成功 = 0
                    $("input[name=regbutton]").get(0).disabled="disabled";
                    needflag1=!0;
                    var target = 'http://my.zhaopin.com/MYZHAOPIN/resume_baseinfo.asp?nr=yes&resume_type=0&nu=yes&ut=' + ((MZ_REG.checkMobile($("#email").val())==0) ? 'm' : '');
                    window.location.assign(target);
                    break;

                    case -1:
                    //参数验证错误 = -1
                    alert("参数验证错误");
                    break;

                    case -2:
                    //请输入手机验证码 = -2
                    verifyCode.setStatus(2);
                    verifyCode.setInfobox();
                    break;

                    case -3:
                    //手机验证码验证失败 = -3
                    verifyCode.setStatus(4);
                    verifyCode.setInfobox();
                    break;

                    case -15:
                    //账户格式错误 = -15
                    email.setStatus(4);
                    email.setInfobox();
                    break;

                    case -32:
                    //图片验证码输入为空 = -32 
                    needflag1=!1;
                    verifyFn.show1();
                    $(".verifyArea1 .imgarea .vimg").trigger("click");
                    mailverifyCode.setStatus(2);
                    mailverifyCode.setInfobox();
                    break;

                    case -33:
                    //图片验证码输入错误 = -33
                    $(".verifyArea1 .imgarea .vimg").trigger("click");
                    mailverifyCode.setStatus(4);
                    mailverifyCode.setInfobox();
                    break;

                    case -41:
                    //用户名不符合规范 = -41,
                    email.setStatus(4);
                    email.setInfobox();
                    break;

                    case -42:
                    //密码不符合规范 = -42
                    password1.setStatus(3);
                    password1.setInfobox();
                    break;

                    case -51:
                    //该用户名已经被注册 = -51
                    email.setStatus(5);
                    email.setInfobox();
                    break;

                    case -31:
                    //申请过于频繁 = -31
                    alert("操作频繁，请稍候再试！");
                    break;

                    case -500:
                    //注册失败
                    alert("注册失败，请稍候再试！");
                    break;
                }

                if(code!==-32&&code!==33&&!needflag1){
                    $(".verifyArea1 .imgarea .vimg").trigger("click");
                }

                return;               
            },
            error: function () {
                alert("注册失败，请重试");
            }
        });
       
    }
}


//登录
function loginAction2(){
    var result = checkLoginForm();
    if(!result){
        return;
    }else{
        var _loginform = $('#loginform');
        _loginform[0].loginbutton.disabled = false;
        var data = {
                LoginName: $("#loginname").val(),
                Password: $("#password").val(),
                CheckCode: $("#_mailverifyCode").val(),
                RememberMe: $("input[name=isautologin]").prop("checked")
            };
        var url = 'http://passport.zhaopin.com/account/loginhandler';
        $.ajax({
            type: "get", 
            dataType: "jsonp",
            async: false,
            url: url,
            data: data,
            success: function (r,s,xhr) {
                var code=r.ErrorCode,errMsg = r.MessageText;
                /*//登录成功 = 0,
                

                //您的IP已经被列为黑名单 = 13,
                //您的邮箱已经被列为黑名单 = 14,
                //您的邮箱域名已经被列为黑名单 = 15,
                //登录过于频繁 = 21,

                //没有找到该用户 = 22,
                //您输入的密码与账号不匹配 = 23,
                //未注册的Email地址 = 30,
                //输入参数错误 = 11,

                //您的账户已经被冻结 = 33,
                //您的账户已经被注销 = 34,
                //您的账户已经被锁定 = 35,
                //您的IP已经被锁定 = 36,

                //验证码输入为空 = 37,
                //验证码验证失败 = 38,

                
                //手机登录失败 = 51

                //您的IP已经被列为黑名单 = 13,
                //您的邮箱已经被列为黑名单 = 14,
                //您的邮箱域名已经被列为黑名单 = 15,
                if(code == 13 || code == 14 || code == 15 || code == 21 || code == 51){
                    alert("登录失败，请稍后重试！");
                    return;
                }*/
                if(code == 11){
                    alert("输入参数错误！");
                    return;
                }

                switch(code){
                    case 0:
                        //登录成功
                        needflag2=!0;
                        $("input[name=loginbutton]").get(0).disabled="disabled";
                        window.location.assign('http://i.zhaopin.com');
                        break;
                    /*case 22:
                        alert("没有找到该用户！");
                        break;*/
                    case 23:
                        //您输入的密码与账号不匹配 = 23
                        password.setStatus(4);
                        password.setInfobox();
                        break;
                    case 30:
                        //未注册的Email地址 = 30
                        loginname.setStatus(5);
                        loginname.setInfobox();
                        break;
                    case 37:
                        //验证码为空 = 37
                        needflag2=!1;
                        verifyFn.show2();
                        $(".verifyArea2 .imgarea .vimg").trigger("click");
                        _mailverifyCode.setStatus(2);
                        _mailverifyCode.setInfobox();
                        break;
                    case 38:
                        //验证码验证失败 = 38
                        $(".verifyArea2 .imgarea .vimg").trigger("click");
                        _mailverifyCode.setStatus(4);
                        _mailverifyCode.setInfobox();
                        break;
                    default :
                        if(code != 22){
                            $("#password_info").addClass('yz_td').html(errMsg);
                        }
                        break;
                }

                if(code!==37&&code!==38&&!needflag2){
                    $(".verifyArea2 .imgarea .vimg").trigger("click");
                }

                return;
            },
            error: function (msg) {
                alert('网络错误，请稍候重试！')
            }
        });
    }
}

//弹窗
$.popTip=function(o){
    var d={
        t:1,
        callback:''
    },
    f=$.extend({},d,o),s=$.popTip;
    if($('.popTip').length==0){
        var html = '<div class="popTip popmask"></div>';
            html += '<div class="popTip popwin">';
            html += '<iframe src="" class="popifr"></iframe>'
            html += '<div class="popTit">';
            html += '<h3>获取短信验证码</h3>';
            html += '<a class="close_c close" title="关闭">X</a>';
            html += '</div>';
            html += '<div class="popcon"></div>';
            html += '<div class="popfoot">';
            html += '<span class="submitcode">提交</span>';
            html += '</div></div>';
            
        $('body').append(html);

        s.m = $('.popTip');
        s.m.a=s.m.filter('.popmask');
        s.m.b=s.m.filter('.popwin');

        s.m.ini=function(t){
            s.m.a.css('height',$(document).height())
            s.m.b.css('top',$(window).scrollTop()+230);
            s.m.find('.popcon').html(t);
            return this;
        }

        s.m.delegate('.close', 'click', function(){s.close();});

        s.close=function(){s.m.remove();}
    }

    s.m.ini(f.t).show();

    if(f.callback!==undefined && typeof(f.callback)=='function') {
        f.g={a:s.m.a[0],b:s.m.b[0]};
        f.callback(f.g);
    }
}

$(".imgarea .vimg").each(function(i,v){
    $(v).click(function(){
        verifyFn.switchImg.call($(this));
    });
});

//短信验证码
var verifyFn={
    show:function(){
        $('.verifyArea').show();
        $('.cont_iput').height(450);
        this.iniBtn();
    },
    hide:function(){
        $('.verifyArea').hide();
        if(!needflag1) $('.cont_iput').height(450);
            else $('.cont_iput').height(385);
        //this.uniniBtn();
    },
    show1:function(){
        $('.verifyArea1').show();
        $('.cont_iput').height(450);
    },
    hide1:function(){
        $('.verifyArea1').hide();
        $('.cont_iput').height(450);
    },
    show2:function(){
        $('.verifyArea2').show();
    },
    hide2:function(){
        $('.verifyArea2').hide();
    },
    iniBtn:function(){
        var i = 60, s = $('.sendBtn');
        var html = [];
            html.push('<table class="code_item">')
            html.push('<tr><td>验证码</td>')
            html.push('<td><input class="inputtxt_1 inputw_1" type="text" name="picVerifyCode" id="picVerifyCode" maxlength="4"></td>')
            html.push('<td><img class="vimg" src="http://passport.zhaopin.com/checkcode/img" alt="验证码" title="刷新验证码" width="80" height="35" /></td></tr>')
            html.push('<tr><td>&nbsp;</td><td><span class="vcode_info"></span></td><td>&nbsp;</td></tr>')
            html.push('</table>')
            html = html.join('');

        s.bind('click.cc', function(){clickfn()});

        function clickfn(){
            $.popTip({
                callback:function(v){
                    function _text(t,c){
                        if(c==0){
                                $(this).text(t).attr('class','yzi_td')
                        }else if(c==1){
                                $(this).text(t).attr('class','yz_td')
                        }
                    }

                    var o = $(v.b), 
                        vimg = o.find('.vimg'), 
                        sub = o.find('.submitcode'), 
                        pic = o.find('#picVerifyCode'), 
                        err = o.find('.vcode_info'),
                        $v = $("#verifyCode_info");
                        vimg.click(function(){verifyFn.switchImg.call(this);});vimg.click();
                        sub.click(function(){
                            var v = pic.val();
                            if(v==''){
                                err.text('请输入验证码').addClass('vcode_error');
                            }else if(!/^[a-zA-Z0-9]{1,4}$/g.test(v)){
                                err.text('验证码格式错误').addClass('vcode_error');
                            }else{
                                err.text('正在校验验证码...').removeClass('vcode_error');
                                //图灵验证码
                                //QA
                                $.ajax({
                                    type: "get",
                                    async:false,
                                    dataType: "jsonp",
                                    url: "http://passport.zhaopin.com/account/sendcheckcodehandler",
                                    data: {"mobile":$("#email").val(),"checkcode":v},
                                    success: function (msg) {
                                        var msg = parseInt(msg.ErrorCode);
                                        if(msg==2){
                                            err.text('验证码错误').addClass('vcode_error');
                                            vimg.click();
                                            return;
                                        }

                                        err.text('验证成功');
                                        sub.unbind('click');
                                        clearTimeout(mymt);
                                        var mymt=setTimeout(function(){
                                            $.popTip.close();
                                            switch(msg){
                                                case 0:
                                                _text.call($v,'验证码已发送，请查收短信！',0);
                                                break;
                                                case 1:
                                                _text.call($v,msg.MessageText,1);
                                                break;
                                                case 3:
                                                _text.call($v,"请输入正确的手机号！",1);
                                                break;
                                                case 4:
                                                _text.call($v,"该手机已注册！",1);
                                                break;
                                            }
                                            octime();
                                        },800);
                                     },
                                    error: function (msg) { 
                                        err.text('网络错误请稍后重试').addClass('vcode_error');
                                        vimg.click();
                                    }
                                });
                            }
                        })
                    },
                t:html
            });
        }

        function octime(){
            if(i>0){
                s.text(i+'秒后重发').addClass('unsendBtn');
                s.unbind('click.cc');
                i--;
                window.setTimeout(function(){
                    s.text(i+'秒后重发');
                    octime();
                }, 1000)
            }else{
                s.text('发送验证码');
                s.bind('click.cc',function(){clickfn()}).removeClass('unsendBtn');
                i=60;
            }
        }
    },
    switchImg:function(){
        var s = $(this),
            src = s.attr('src');
            src = src.substring(0,(src.indexOf('?')==-1)?src.length:src.indexOf('?'));
            s.attr('src',src+'?'+new Date().getTime().toString());
    }
   
}

